package cn.wolfcode.edu.web.controller;

import cn.wolfcode.edu.anno.PermissionName;
import cn.wolfcode.edu.domain.Client;
import cn.wolfcode.edu.query.ClientQueryObject;
import cn.wolfcode.edu.service.IClientService;
import cn.wolfcode.edu.util.JsonResult;
import cn.wolfcode.edu.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    private IClientService clientService;

    //定位客户主界面
    @RequestMapping("")
    public String gotoClientPage(){
        return "client/client";
    }

    //给client_datagrid 传递数据
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ClientQueryObject qo){
        return clientService.query(qo);
    }

    //输出资源池的数据
    @RequestMapping("listNoSaleman")
    @ResponseBody
    public PageResult listNoSaleman(ClientQueryObject qo){
        return clientService.listNoSaleman(qo);
    }

    //新增用户列表
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Client client){
        try {
            clientService.save(client);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("保存失败");
        }
    }

    //编辑用户
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Client client){
        try {
            clientService.edit(client);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("保存失败");
        }
    }

    //根据指定id查询客户对象信息
    @RequestMapping("getClientById")
    @ResponseBody
    public Client getClientById(Long clientId){
        return clientService.getClientById(clientId);
    }

    //放入资源池
    @RequestMapping("abandon")
    @ResponseBody
    public JsonResult abandon(Long id){
        try {
            clientService.abandon(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("放入失败");
        }
    }

    //表格导出
    //表格导出
    @RequestMapping("export")
    @ResponseBody
    public void export(ClientQueryObject qo, HttpServletResponse response) throws Exception {
        //1.查询出列表数据
        qo.setRows(1000);
        List clients = clientService.queryForList(qo);
        response.setHeader("Content-Disposition", "attachment;filename=client.xls");
        OutputStream outputStream = response.getOutputStream();
        //创建一个 工作簿,目标是outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
        //2.处理标题
        //创建一个可以写sheet
        WritableSheet sheet = workbook.createSheet("clients", 0);
        //创建一个单元格
        Label idLabel = new Label(0, 0, "客户编号");
        Label nameLabel = new Label(1, 0, "姓名");
        Label realNameLabel = new Label(2, 0, "营销人员");
        Label traceNumLabel = new Label(3, 0, "跟踪次数");
        Label lastTimeLabel = new Label(4, 0, "最后跟踪时间");
        Label appointTimeLabel = new Label(5, 0, "约访时间");
        Label nextTimeLabel = new Label(6, 0, "下次跟进时间");
        Label jobLabel = new Label(7, 0, "现在做什么");
        Label qqSalaryLabel = new Label(8, 0, "QQ");
        Label telLabel = new Label(9, 0, "电话");
        Label schoolLabel = new Label(10, 0, "学校");
        Label degreeLabel = new Label(11, 0, "意向程度");
        Label campusLabel = new Label(12, 0, "意向校区");
        Label clzLabel = new Label(13, 0, "意向班级");
        Label statueLabel = new Label(14, 0, "状态");
        Label traceLabel = new Label(15, 0, "是否跟踪");
        Label remarkLabel = new Label(16, 0, "备注");
        //添加标题
        sheet.addCell(idLabel);
        sheet.addCell(nameLabel);
        sheet.addCell(realNameLabel);
        sheet.addCell(traceNumLabel);
        sheet.addCell(lastTimeLabel);
        sheet.addCell(appointTimeLabel);
        sheet.addCell(nextTimeLabel);
        sheet.addCell(jobLabel);
        sheet.addCell(qqSalaryLabel);
        sheet.addCell(telLabel);
        sheet.addCell(schoolLabel);
        sheet.addCell(degreeLabel);
        sheet.addCell(campusLabel);
        sheet.addCell(statueLabel);
        sheet.addCell(clzLabel);
        sheet.addCell(traceLabel);
        sheet.addCell(remarkLabel);
        //3.循环迭代list,每一个client对象对应一行,每个字段对应一行的一列
        for (int i = 0; i < clients.size(); i++) {
            Client client = (Client) clients.get(i);
            int row = i + 1;
            //填充数据
            Label tempidLabel = new Label(0, row, client.getId().toString());
            Label tempNameLabel = new Label(1, row, client.getName().toString());
            Label temprealNameLabel = new Label(2, row, client.getSaleman().getRealName());
            Label tempTraceNumLabel = new Label(3, row, client.getTraceNum().toString());
            Label tempLastTimeLabel = new Label(4, row, client.getLastTraceTime().toLocaleString());
            Label tempAppointTimeLabel = new Label(5, row, client.getAppointTime().toLocaleString());
            Label tempnextTimeLabel = new Label(6, row, client.getNextTraceTime().toLocaleString());
            Label tempupJobLabel = new Label(7, row, client.getJob());
            Label tempqqDaysLabel = new Label(8, row, client.getQq());
            Label tempTelsLabel = new Label(9, row, client.getTel());
            Label tempSchoolLabel = new Label(10, row, client.getSchool());
            Label tempDegreeLabel = new Label(11, row, client.getIntentionDegree().getName());
            Label tempCampusLabel = new Label(12, row, client.getCampus().getName());
            Label tempClzLabel = new Label(13, row, client.getClz().getName());
            Label tempStatueLabel = new Label(14, row, client.getStatue().getName());
            String tempTrace;
            if (!client.isTraced()) {
                tempTrace = "待跟踪";
            } else {
                tempTrace = "已跟踪";
            }
            Label tempTraceLabel = new Label(15, row, tempTrace);
            Label tempRemarkLabel = new Label(16, row, client.getRemark());
            sheet.addCell(tempidLabel);
            sheet.addCell(tempNameLabel);
            sheet.addCell(temprealNameLabel);
            sheet.addCell(tempTraceNumLabel);
            sheet.addCell(tempLastTimeLabel);
            sheet.addCell(tempAppointTimeLabel);
            sheet.addCell(tempnextTimeLabel);
            sheet.addCell(tempupJobLabel);
            sheet.addCell(tempqqDaysLabel);
            sheet.addCell(tempTelsLabel);
            sheet.addCell(tempSchoolLabel);
            sheet.addCell(tempDegreeLabel);
            sheet.addCell(tempCampusLabel);
            sheet.addCell(tempClzLabel);
            sheet.addCell(tempStatueLabel);
            sheet.addCell(tempTraceLabel);
            sheet.addCell(tempRemarkLabel);
        }
        //把内容中的工作簿对象的相关内容写到 输出流 里面
        workbook.write();
        //关闭工作簿
        workbook.close();
    }



}
